package com.ruigi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruigi.Dto.DishDto;
import com.ruigi.mapper.Dishmapper;
import com.ruigi.pojo.Category;
import com.ruigi.pojo.Dish;
import com.ruigi.pojo.DishFlavor;
import com.ruigi.pojo.R;
import com.ruigi.service.CategoryService;
import com.ruigi.service.DishflaverService;
import com.ruigi.service.Dishservice;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service

public class Dishimpl extends ServiceImpl<Dishmapper, Dish> implements Dishservice {
    @Autowired
    private DishflaverService dishflaverService;
    @Autowired
    private CategoryService categoryService;


    @Override
    @Transactional
    public void savewithfalor(DishDto dishDto) {
        this.save(dishDto);
        Long dishid = dishDto.getId();

        List<DishFlavor> flavors = dishDto.getFlavors();
        List<DishFlavor> collect = flavors.stream().map((dishFlavor) -> {
            dishFlavor.setDishId(dishid);
            return dishFlavor;

        }).collect(Collectors.toList());
        dishflaverService.saveBatch(collect);

    }

    @Override
    public R<Page> page1(int page, int pageSize, String name) {

        Page<Dish> dishPage = new Page<>(page, pageSize);
        Page<DishDto> dtopage = new Page<>();
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Dish::getName, name);
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        this.page(dishPage, queryWrapper);

        BeanUtils.copyProperties(dishPage, dtopage, "records");

        List<Dish> records = dishPage.getRecords();
        List<DishDto> collect = records.stream().map(re -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(re, dishDto);

            Long id = re.getCategoryId();
            Category byId = categoryService.getById(id);
            if (byId != null) {
                String name1 = byId.getName();
                dishDto.setCategoryName(name1);

            }

            return dishDto;
        }).collect(Collectors.toList());
        dtopage.setRecords(collect);

        return R.success(dtopage);
    }

    @Override
    public R<DishDto> getid(Long id) {
        Dish byId = this.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(byId, dishDto);


        //query for the dishflavor

        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, byId.getId());
        List<DishFlavor> list = dishflaverService.list(queryWrapper);
        dishDto.setFlavors(list);

        return R.success(dishDto);
    }

    @Override
    public R<String> updateflavor(DishDto dishDto) {
        this.updateById(dishDto);

        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishflaverService.remove(queryWrapper);


        List<DishFlavor> flavors = dishDto.getFlavors();
        List<DishFlavor> collect = flavors.stream().map(s -> {
            s.setDishId(dishDto.getId());
            return s;


        }).collect(Collectors.toList());

        dishflaverService.saveBatch(collect);


        return null;
    }


}
